<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="canonical" href="https://example.com/fs.html" />
      <link rel="shortcut icon" href="img/favicon.ico" />
    <title>Filesystem: manage folders and files - ten's documentation</title>
    <link rel="stylesheet" href="css/theme.css" />
    <link rel="stylesheet" href="css/theme_extra.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" />
    
      <script>
        // Current page data
        var mkdocs_page_name = "Filesystem: manage folders and files";
        var mkdocs_page_input_path = "fs.md";
        var mkdocs_page_url = "/fs.html";
      </script>
    
    <script src="js/jquery-3.6.0.min.js" defer></script>
    <!--[if lt IE 9]>
      <script src="js/html5shiv.min.js"></script>
    <![endif]-->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
      <script>hljs.initHighlightingOnLoad();</script> 
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
    <div class="wy-side-scroll">
      <div class="wy-side-nav-search">
          <a href="index.html" class="icon icon-home"> ten's documentation
        </a><div role="search">
  <form id ="rtd-search-form" class="wy-form" action="./search.html" method="get">
      <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="index.html">Home</a>
                </li>
              </ul>
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="flow.html">Flow: Input, output, and flow control</a>
                </li>
              </ul>
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="transform.html">Transform: Convert data</a>
                </li>
              </ul>
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="http.html">HTTP: requests on steroids</a>
                </li>
              </ul>
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="shell.html">Shell: run commands</a>
                </li>
              </ul>
              <ul class="current">
                <li class="toctree-l1 current"><a class="reference internal current" href="fs.html">Filesystem: manage folders and files</a>
    <ul class="current">
    <li class="toctree-l2"><a class="reference internal" href="#files">Files</a>
        <ul>
    <li class="toctree-l3"><a class="reference internal" href="#read-and-write">Read and write</a>
    </li>
    <li class="toctree-l3"><a class="reference internal" href="#creating-a-file">Creating a file</a>
    </li>
        </ul>
    </li>
    <li class="toctree-l2"><a class="reference internal" href="#directories">Directories</a>
    </li>
    </ul>
                </li>
              </ul>
              <ul>
                <li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorials and examples</a>
                </li>
              </ul>
      </div>
    </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
      <nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">ten's documentation</a>
        
      </nav>
      <div class="wy-nav-content">
        <div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="index.html" class="icon icon-home" alt="Docs"></a> &raquo;</li>
      <li>Filesystem: manage folders and files</li>
    <li class="wy-breadcrumbs-aside">
    </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
            <div class="section" itemprop="articleBody">
              
                <h1 id="filesystem-files-directories">Filesystem (files, directories)</h1>
<p>Quickly access and edit files. Refer to <a href="../tenlib/fs.html"><code>tenlib.fs</code>'s documentation</a> for details.</p>
<p>The FS module has a few functions to quickly manipulate files:</p>
<pre><code class="language-python">from ten import *

if exists(&quot;/tmp/test.txt&quot;):
    data = read_text(&quot;/tmp/test.txt&quot;)
else:
    data = compute()
    write(&quot;/tmp/test.txt&quot;, data)
</code></pre>
<p>For advanced usage, use <a href="../tenlib/fs.html#tenlib.fs.Path"><code>Path</code></a> instead:</p>
<pre><code class="language-python">path = Path(&quot;/tmp/test.txt&quot;)

if path.exists():
    data = path.read_text()
else:
    data = compute()
    path.write(data)
</code></pre>
<h2 id="files">Files</h2>
<p>Create a <code>Path</code> object, a wrapper for <code>pathlib.Path</code> with a few additional methods.</p>
<pre><code class="language-python">p = Path(&quot;/tmp/test.txt&quot;)
</code></pre>
<h3 id="read-and-write">Read and write</h3>
<p>To read data as a string or bytes:</p>
<pre><code class="language-python">contents = p.read_text()
contents = p.read_bytes()
</code></pre>
<p>To write data as bytes or string, use the <code>Path.write()</code> method.</p>
<pre><code class="language-python">p.write(b&quot;contents&quot;)
</code></pre>
<pre><code class="language-python">p.write(&quot;contents&quot;)
</code></pre>
<p>To append, use <code>Path.append()</code>:</p>
<pre><code class="language-python">p.append(&quot;additional contents&quot;)
</code></pre>
<h3 id="creating-a-file">Creating a file</h3>
<p>Create a file using <code>Path.touch()</code>.</p>
<pre><code class="language-python">p.touch()
</code></pre>
<p>To create the whole directory hierarchy, use <code>parents=True</code>, or simply <code>ptouch()</code>:</p>
<pre><code class="language-python">p = Path(&quot;/tmp/long/path/to/file&quot;)

# equivalent
p.touch(parents=True)
p.ptouch()
</code></pre>
<p>The call returns the instance, so it can be chained with <code>write()</code>, for instance.</p>
<pre><code class="language-python">p.ptouch().write(&quot;data&quot;)
</code></pre>
<h2 id="directories">Directories</h2>
<p>To go through a directory, use <code>glob()</code> or <code>rglob()</code>.</p>
<pre><code class="language-python">dir = Path(&quot;./dir&quot;)

for file in dir.rglob(&quot;**/*.py&quot;):
    msg_info(f&quot;Found python file: {file}&quot;)
    process_file(file)
</code></pre>
<p>Create a directory using <code>mkdir()</code>:</p>
<pre><code class="language-python">dir = Path(&quot;./dir&quot;)
dir.mkdir()
</code></pre>
<p>To create the parent directories as well, use <code>mkdir(parent=True)</code>.</p>
              
            </div>
          </div><footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
        <a href="shell.html" class="btn btn-neutral float-left" title="Shell: run commands"><span class="icon icon-circle-arrow-left"></span> Previous</a>
        <a href="tutorial.html" class="btn btn-neutral float-right" title="Tutorials and examples">Next <span class="icon icon-circle-arrow-right"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
  </div>

  Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
          
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" aria-label="Versions">
  <span class="rst-current-version" data-toggle="rst-current-version">
    
    
      <span><a href="shell.html" style="color: #fcfcfc">&laquo; Previous</a></span>
    
    
      <span><a href="tutorial.html" style="color: #fcfcfc">Next &raquo;</a></span>
    
  </span>
</div>
    <script>var base_url = '.';</script>
    <script src="js/theme_extra.js" defer></script>
    <script src="js/theme.js" defer></script>
      <script src="search/main.js" defer></script>
    <script defer>
        window.onload = function () {
            SphinxRtdTheme.Navigation.enable(true);
        };
    </script>

</body>
</html>
